<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { CustomMouseMenu } from '@/plugins/directive/contextmenu';
import { ElMessageBox, ElMessage } from 'element-plus';
import useStore from '@/store';
import { MasterEnum } from '@/enums';
import { LicenseEnum } from '@/enums/licence';
import { props } from './warehouse';
import { WindowType } from '@/types/global/window';
defineOptions({ name: 'Warehouse' });
defineProps(props);
const tableRef = ref(null);
const loading = ref(true);
const warehouses = ref(null);
const currentRow = ref(null);
const totalCount = ref(0);
const params = reactive({
  pageIndex: 1,
  pageSize: 20,
  query: {
    code: null,
    name: null,
  },
});

const handleTableRowClick = row => {
  currentRow.value = row;
};
const remove = () => {
  if (!currentRow.value) {
    ElMessage.error('请选择要删除的仓库!');
    return;
  }
  let confirmMessage = '是否删除选中的模块?';
  // if (ids.constructor === Number) {
  //   confirmMessage = '是否删除模块（' + name + '）?';
  //   ids = [ids];
  // }

  ElMessageBox.confirm(confirmMessage, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // window.$api.module.remove(ids).then(res => {
      //   ElMessage.success('删除成功');
      //   query();
      // });
    })
    .catch(() => {});
};
const query = async () => {
  loading.value = true;
  await window.$api.warehouse.query(params).then(res => {
    warehouses.value = res.data.list;
    params.pageIndex = res.data.pageIndex;
    params.pageSize = res.data.pageSize;
    totalCount.value = res.data.totalCount;
    loading.value = false;
  });
};
const add = () => {
  const window: WindowType = { name: 'WarehouseForm', component: 'WarehouseForm', label: '仓库添加', icon: 'edit', width: 500, height: 500, callBack: () => query() };
  useStore.window.windowAdd(window);
};
const edit = () => {
  if (!currentRow.value) {
    ElMessage.error('请选择要修改的仓库!');
    return;
  }
  const window: WindowType = {
    name: 'WarehouseForm' + currentRow.value.id,
    component: 'WarehouseForm',
    label: '仓库修改（' + currentRow.value.name + '）',
    icon: 'edit',
    width: 500,
    height: 500,
    params: { id: currentRow.value.id },
    callBack: () => query(),
  };
  useStore.window.windowAdd(window);
};

const menus = reactive([
  {
    label: '修改',
    icon: 'Edit',
    fn: () => {
      edit();
    },
    license: LicenseEnum.SYS_WAREHOUSE_MODIFY,
  },
  {
    label: '删除',
    icon: 'Delete',
    fn: () => {
      remove();
    },
    license: LicenseEnum.SYS_WAREHOUSE_REMOVE,
  },
]);
const showMouseMenu = (row, column, event) => {
  const ctx = CustomMouseMenu({
    el: event.currentTarget,
    mouse: event,
    ...{ menus },
  });
  ctx.$root.show();
  currentRow.value = row;
  tableRef.value.setCurrentRow(row);
};
onMounted(() => {
  query();
});
</script>

<template>
  <windows-window :window="window" class="warehouse">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-apps-add-in-24-regular" :license="LicenseEnum.SYS_WAREHOUSE_ADD" @click="add">新建</windows-button>
        <windows-button icon="fluent-draw-shape-24-regular" :license="LicenseEnum.SYS_WAREHOUSE_MODIFY" split @click="edit">编辑</windows-button>
        <windows-button icon="fluent-delete-28-regular" :license="LicenseEnum.SYS_WAREHOUSE_REMOVE">删除</windows-button>
      </windows-button-group>
    </template>
    <template #search>
      <el-form ref="form" :model="params.query" label-width="auto" label-position="right" size="small">
        <el-row>
          <el-col :span="10">
            <el-form-item label="仓库代码">
              <el-input v-model="params.query.code" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="仓库名称">
              <el-input v-model="params.query.name" />
            </el-form-item>
          </el-col>
          <el-col :span="4" style="text-align: right">
            <el-button type="primary" icon="Search" @click="query">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <el-table ref="tableRef" :data="warehouses" style="width: 100%" height="100%" highlight-current-row v-loading="loading" @row-contextmenu="showMouseMenu" @row-click="handleTableRowClick">
      <el-table-column type="index" width="80" label="序号" align="center" />
      <el-table-column prop="code" label="仓库代码" align="center" width="100">
        <template #default="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="仓库名称" align="center" minWidth="150"></el-table-column>
      <el-table-column prop="address" label="仓库地址" align="center" minWidth="150" show-overflow-tooltip></el-table-column>
      <el-table-column prop="manageManName" label="管理人" align="center" minWidth="100"></el-table-column>
      <el-table-column prop="telephone" label="联系电话" align="center" minWidth="100"></el-table-column>
      <el-table-column prop="attribute" label="仓库属性" align="center" minWidth="100"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" minWidth="100"></el-table-column>
    </el-table>
    <template #pagination>
      <el-pagination
        v-model:currentPage="params.pageIndex"
        v-model:page-size="params.pageSize"
        :page-sizes="[20, 30, 40, 50]"
        small
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        @size-change="query"
        @current-change="query"
      />
    </template>
  </windows-window>
</template>

<style scoped lang="scss">
.el-tree {
  width: 180px;
}
</style>
